package com.midian.model.user;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.json.annotations.JSON;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.midian.common.system.SysStatic;
import com.midian.common.utils.StringUtil;
import com.midian.model.common.BaseModel;
import com.midian.model.pic.Pic;
import com.midian.service.user.IUserService;

/**
 * 用户（工人/企业）
 * 
 * @author Mengfh
 * 
 */
@Entity
@Table(name = "user")
public class User extends BaseModel {
	private static final long serialVersionUID = 1L;
	/** 第三方唯一标识 */
	private String thirdId;
	/** 姓名 */
	private String name;
	/** 账号 */
	private String account;
	/** 密码 */
	@Column(updatable = false)
	private String password;

	/** 生日 */
	private Date birthday;

	/** 电话 */
	private String phone;

	/** 用户所绑定的角色列表 */
	@Transient
	private List<String> roleIdList;
	/** 头像 */
	@ManyToOne(optional = true,fetch=FetchType.LAZY)
	@JoinColumn(name = "headPicId")
	@NotFound(action=NotFoundAction.IGNORE)
	private Pic headPicId;

	/** 1：正常注册 2:微信 3:qq */
	private Integer userfrom = 1;

	/** 性别 2:女 1：男 */
	private Integer sex;

	/** 身份1:管理员 2：普通用户 */
	private Integer identity;

	/** token */
	private String token;
	/** token生成时间 */
	private Date tokenTime;
	/** 是否为管理员 1：是 2：否 */
	private Integer isAdmin = 2;

	public User() {
		this.userfrom = null;
		this.isAdmin = null;
		super.setDelFlag(null);
	}

	public String getThirdId() {
		return thirdId;
	}

	public void setThirdId(String thirdId) {
		this.thirdId = thirdId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public Pic getHeadPicId() {
		return headPicId;
	}

	public void setHeadPicId(Pic headPicId) {
		this.headPicId = headPicId;
	}

	/**
	 * 帐号描述信息
	 * 
	 * @return
	 */
	public String getAccountDesc() {
		String accountDesc = "";
		if (IUserService.USER_FROM_NORMAL.equals(this.userfrom)
				&& StringUtils.isNotBlank(this.account)) {
			accountDesc = this.account;
		} else if (IUserService.USER_FROM_QQ.equals(this.userfrom)) {
			accountDesc = "QQ用户";
		} else if (IUserService.USER_FROM_WECHAT.equals(this.userfrom)) {
			accountDesc = "微信用户";
		}
		return accountDesc;
	}

	/**
	 * 用户头像描述
	 * 
	 * @return
	 */
	public String getHeadPicIdTip() {
		String headPortraitTip = "";
		if (this.headPicId != null) {
			headPortraitTip = StringUtil.getImageTips(this.headPicId
					.getId() + this.headPicId.getSuffix());
		}
		return headPortraitTip;
	}

	public Integer getUserfrom() {
		return userfrom;
	}

	public void setUserfrom(Integer userfrom) {
		this.userfrom = userfrom;
	}

	/**
	 * 用户来源
	 * 
	 * @return
	 */
	public String getUserfromDesc() {
		String userfromDesc = "";
		if (IUserService.USER_FROM_NORMAL.equals(this.userfrom)) {
			userfromDesc = "默认";
		} else if (IUserService.USER_FROM_WECHAT.equals(this.userfrom)) {
			userfromDesc = "微信";
		} else if (IUserService.USER_FROM_QQ.equals(this.userfrom)) {
			userfromDesc = "QQ";
		}
		return userfromDesc;
	}

	@JSON(format = "yyyy-MM-dd")
	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Integer getSex() {
		return sex;
	}

	public void setSex(Integer sex) {
		this.sex = sex;
	}

	/**
	 * 是否管理员描述
	 * 
	 * @return
	 */
	public String getIsAdminDesc() {
		String isAdminDesc = "";
		if (SysStatic.NORMALDELFLAG.equals(this.isAdmin)) {
			isAdminDesc = "是";
		} else {
			isAdminDesc = "否";
		}
		return isAdminDesc;
	}

	/**
	 * 性别描述
	 * 
	 * @return
	 */
	@Transient
	public String getSexDesc() {
		String sexDesc = "";
		if (IUserService.SEX_MALE.equals(this.sex)) {
			sexDesc = "男";
		} else if (IUserService.SEX_FEMALE.equals(this.sex)) {
			sexDesc = "女";
		}
		return sexDesc;
	}

	public List<String> getRoleIdList() {
		return roleIdList;
	}

	public void setRoleIdList(List<String> roleIdList) {
		this.roleIdList = roleIdList;
	}

	public Integer getIdentity() {
		return identity;
	}

	public void setIdentity(Integer identity) {
		this.identity = identity;
	}

	/**
	 * 会员类型描述
	 * 
	 * @return
	 */
	public String getIdentityDesc() {
		String identityDesc = "";
		if (IUserService.IDENTITY_MANAGER.equals(this.identity)) {
			identityDesc = "管理员";
		} else if (IUserService.IDENTITY_ELSE.equals(this.identity)) {
			identityDesc = "会员";
		}
		return identityDesc;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public Date getTokenTime() {
		return tokenTime;
	}

	public void setTokenTime(Date tokenTime) {
		this.tokenTime = tokenTime;
	}

	public Integer getIsAdmin() {
		return isAdmin;
	}

	public void setIsAdmin(Integer isAdmin) {
		this.isAdmin = isAdmin;
	}
	
	/**
	 * 会员名称描述（如果获取会员帐号为空，就获取昵称）
	 * @return
	 */
	@Transient
	public String getMemberNameDesc() {
		String memberNameDesc = "";
		if(StringUtils.isNotBlank(this.account)) {
			memberNameDesc = this.account;
		} else {
			memberNameDesc = this.name;
		}
		return memberNameDesc;
	}
	
}
